model{
  for(i in 1:n.obs){  #likelihood
      d.credit[i] ~ dnorm(mu[i], tau.y[i])
      mu[i] <- a.unit[country[i]] + b.l.credit[country[i]]*lag.credit[i] + XB[i]
      XB[i] <- b.l.top1*lag.ineq[i] + b.d.top1*d.ineq[i]+   
        b.cl*cum.left[i] + b.l.top1cl*lag.ineq[i]*cum.left[i] + b.d.top1cl*d.ineq[i]*cum.left[i]+
        b.l.unemp*lag.unemp[i] + b.d.unemp*d.unemp[i] +
        b.l.pop*lag.pop[i] + b.d.pop*d.pop[i] +
        b.l.gdp*lag.gdp[i] + b.d.gdp*d.gdp[i] + #b.d.growth*d.growth[i] +
        b.l.k*lag.k[i] + b.d.k*d.k[i] +
        b.l.cab*lag.cab[i] + b.d.cab*d.cab[i] + 
        b.l.budg*lag.budg[i] + b.d.budg*d.budg[i] +
        b.l.world.save*lag.world.save[i] + b.d.world.save*d.world.save[i] + 
        b.l.bm*lag.bm.growth[i] + b.d.bm*d.bm.growth[i] +
        b.l.old*lag.old[i] + b.d.old*d.old[i]    
    logsigma2.y[i] <- g.unit[country[i]] + g.year[years.d[i]] + g.euro*eurozone[i] #model for the variance
    tau.y[i] <- 1/exp(logsigma2.y[i])
    res[i]<-d.credit[i] - mu[i] #residuals
    y.pred[i] ~ dnorm(mu[i], tau.y[i]) #repredicting y  
  }
  
  for(j in 1:(n.countries)){#country RE in model for variance  
    g.unit[j] ~ dnorm(0, tau.vc)
    a.unit[j] ~ dnorm(mu.c, tau.country) #country RE
    b.l.credit[j] <- -1*temp[j]
    temp[j] ~ dbeta(1,1)
  }
  for(j in 1:n.years){#country RE in model for variance
    g.year[j] ~ dnorm(0, tau.vy) 
  }
  
# priors
  mu.c ~ dnorm(0,0.00001) #country RE
  ## regression parameters
  b.l.top1~ dnorm(0,0.0001)
  b.d.top1~ dnorm(0,0.0001)
  b.cl~ dnorm(0,0.0001)
  b.l.top1cl~ dnorm(0,0.0001)
  b.d.top1cl~ dnorm(0,0.0001)
  b.l.unemp~ dnorm(0,0.0001)
  b.d.unemp~ dnorm(0,0.0001)
  b.l.pop~ dnorm(0,0.0001)
  b.d.pop~ dnorm(0,0.0001)
  b.l.gdp~ dnorm(0,0.0001)
  b.d.gdp~ dnorm(0,0.0001)
  b.l.k~ dnorm(0,0.0001)
  b.d.k~ dnorm(0,0.0001)
  b.l.cab~ dnorm(0,0.0001)
  b.d.cab~ dnorm(0,0.0001)
  b.l.budg~ dnorm(0,0.0001)
  b.d.budg~ dnorm(0,0.0001)
  b.l.world.save~ dnorm(0,0.0001)
  b.d.world.save~ dnorm(0,0.0001)
  b.l.bm~ dnorm(0,0.0001)
  b.d.bm~ dnorm(0,0.0001)
  b.l.old ~ dnorm(0,0.0001)
  b.d.old ~ dnorm(0,0.0001)
  g.euro ~dnorm(0,0.0001)
  ## variances/precisions
  sigma.country ~ dunif(0,50)
  tau.country <- pow(sigma.country,-2)
  sigma.vc ~ dunif(0,10) #variance for country RE in variance term
  sigma.vy ~ dunif(0,10) #variance for year RE in variance term
  tau.vc <- pow(sigma.vc, -2)
  tau.vy <- pow(sigma.vy, -2)
}
